package com.vecspace.dayu.code.dao;

import java.util.List;
import org.springframework.stereotype.Repository;

import com.vecspace.database.dao.BaseDao;
import com.vecspace.dayu.code.pojo.ProjectUser;

/** DAYU-项目用户。暂不验证密码，输入用户名即可进入,loginId与oa里面的loginId保持一致。以后可考虑同步OA,操作数据库的dao类
 * @author: wanghua
 * @reviewer:
 * @template：wanghua
 */
@Repository("dayu_projectUserDao")
public class ProjectUserDao extends BaseDao {
	/** 根据ID查询 DAYU-项目用户。暂不验证密码，输入用户名即可进入,loginId与oa里面的loginId保持一致。以后可考虑同步OA
	 * @param id   主键值
	 * @return ProjectUser
	 * @throws Exception
	 * @author wanghua
	 */
	public ProjectUser getProjectUser(Long id) throws Exception {
		ProjectUser projectUser = null;
		if (id != null && id != 0) {

			projectUser = this.queryObject(ProjectUser.class, id);
			this.evict(projectUser);
		}
		return projectUser;
	}
	
	/**判断用户id是否存在于任何项目（或者超级管理员）
	 * 
	 * @param loginId
	 * @return 
	 * @author wanghua
	 * @throws Exception
	 */
	public boolean hasUser(String loginId) throws Exception {
		String sql = "select count(*) from DAYU_CODE_PROJECT_USER where login_id=?";
		int rcount = this.queryInt(sql, new Object[] {loginId});
		return rcount > 0;
	}
	
	public ProjectUser getProjectUser(String loginId, Long projectId) throws Exception {
		ProjectUser projectUser = this.queryObject("from ProjectUser where loginId=?0 and projectId=?1", new Object[] {loginId, projectId});
		return projectUser;
	}
	
	/**获取项目中的所有用户
	 * 
	 * @param projectId
	 * @return
	 * @throws Exception 
	 * @author wanghua
	 */
	public List<ProjectUser> getProjectUsers(Long projectId) throws Exception {
		List<ProjectUser> projectUsers = this.queryObjectList("from ProjectUser where projectId=?0 order by loginId", new Object[] {projectId});
		return projectUsers;
	}

	/** 插入或修改 DAYU-项目用户。暂不验证密码，输入用户名即可进入,loginId与oa里面的loginId保持一致。以后可考虑同步OA 
	 * @param item	要保存的对象
	 * @return void
	 * @throws Exception
	 */
	public void saveProjectUser(ProjectUser item) throws Exception {
		this.saveOrUpdateObject(item);
		this.evict(item);
	}

	/** 根据ID删除 DAYU-项目用户。暂不验证密码，输入用户名即可进入,loginId与oa里面的loginId保持一致。以后可考虑同步OA 
	 * @param id	主键值
	 * @return void
	 * @throws Exception
	 */
	public void deleteProjectUser(Long id) throws Exception {
		if (id != null && id != 0) {
			this.deleteObject(ProjectUser.class, id);
		}
	}
}
